package ztime

import "time"

// WeekStart 获取每周的第一天开始时间
func WeekStart(t time.Time, mondayStart bool) time.Time {
	dtWeekday := t.Weekday()
	daysReduce := -int(dtWeekday)
	if mondayStart {
		daysReduce += 1
		if dtWeekday == time.Sunday {
			daysReduce -= 7
		}
	}
	dt := t.Add(time.Duration(daysReduce) * 24 * time.Hour)
	dt = DayStart(dt)
	return dt
}

// WeekEnd 周结束时间
func WeekEnd(t time.Time, mondayStart bool) time.Time {
	dt := WeekStart(t, mondayStart)
	dt = dt.Add(time.Duration(8)*24*time.Hour - time.Second)
	return dt
}

// SameWeek 判断同一周
func SameWeek(t1, t2 time.Time) bool {
	dt1 := WeekStart(t1, true)
	dt2 := WeekStart(t2, true)
	return SameDay(dt1, dt2)
}
